﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using System.Threading.Tasks;

namespace DetectApp
{
    public partial class SystemSetViewModel : ObservableObject
    {
        [ObservableProperty]
        RatioParam _ratioPara;
        public SystemSetViewModel()
        {
           var filepath = $"Products//ratioParam.json";
            if (File.Exists(filepath))
            {
                using var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                _ratioPara = JsonSerializer.Deserialize<RatioParam>(stream) ?? new();
            }
            else
            {
                _ratioPara = new RatioParam();
            }

           GlobalData.Instance.RatioParam = _ratioPara;
        }

        [RelayCommand]
        public void SaveParam()
        {
            if (GlobalData.Instance.LoginPeople!="工程师")
            {
                MessageBox.Show("无操作权限，请切换工程师账号！");
                return;
            }


            string filePath = Path.Combine("Products", "ratioParam.json");
            if (File.Exists(filePath)) File.Delete(filePath);
            using FileStream fileStream = new(filePath, FileMode.OpenOrCreate, FileAccess.Write);
            JsonSerializer.Serialize(fileStream, _ratioPara, new JsonSerializerOptions
            {
                WriteIndented = true,
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
            });

            GlobalData.Instance.RatioParam = _ratioPara;
        }
    }

    public class RatioParam
    {
        public double X5Ratio { get; set; }
        public double X10Ratio { get; set; }
        public double X20Ratio { get; set; }
        public double X50Ratio { get; set; }
    }
}
